package com.dj.shop.controller;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @author luorui luorui@paradigmrt.com
 * @ClassName BaseController
 * @Description 公共控制
 * @date 2017-10-19
 */
public class BaseController implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     * 返回前端状态码
     */
    String code;
    /**
     * 返回前端错误信息
     */
    String errMsg;


    public Integer getPageSize(Integer pageSize) {
        if(pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        return pageSize;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }


    /**
     * @param pageNo
     * @Description: 初始值
     * @reutrn Integer
     * @author luorui
     * @date 2017-10-27
     */
    public Integer getPageNo(Integer pageNo) {
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        return pageNo;
    }

    /**
     * @description：把浏览器参数转化放到Map集合中
     * @param: request
     * @return： java.util.Map<java.lang.String,java.lang.Object>
     * @author：luorui
     * @date：2018-05-25 15:12
     */
    protected Map<String, Object> getParam(HttpServletRequest request) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String method = request.getMethod();
        Enumeration<?> keys = request.getParameterNames();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            if (key != null) {
                if (key instanceof String) {
                    String value = request.getParameter(key.toString());
                    if ("GET".equals(method)) {//前台encodeURIComponent('我们');转码后到后台还是ISO-8859-1，所以还需要转码
                        try {
                            value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    paramMap.put(key.toString(), value);
                }
            }
        }
        return paramMap;
    }

    /**
     * @description：将数据刷新写回web端
     * @param: response
     * @param: responseContent
     * @return： void
     * @author：luorui
     * @date：2018-05-25 15:12
     */
    protected void flushResponse(HttpServletResponse response, String responseContent) {
        PrintWriter writer = null;
        try {
            response.setCharacterEncoding("GBK");
            // 针对ajax中页面编码为GBK的情况，一定要加上以下两句 
            response.setHeader("Cache-Control", "no-cache");
            response.setContentType("text/html;charset=UTF-8");
            writer = response.getWriter();
            if (responseContent == null || "".equals(responseContent) || "null".equals(responseContent)) {
                writer.write("");
            } else {
                writer.write(responseContent);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * @description：返回列表分页的下角页码
     * @param: total
     * @param: startIndex
     * @param: pageSize
     * @param: result
     * @return： org.springframework.web.servlet.ModelAndView
     * @author：luorui
     * @date：2018-05-25 15:13
     */
    public ModelAndView getPageNumberInfo(int total, int startIndex, int pageSize, ModelAndView result) {
        //Math.ceil整数则为该整数，Math.ceil小数则为靠近大的整数
        int current = (int) Math.ceil((startIndex + 1.0) / pageSize);
        result.addObject("start", startIndex);
        result.addObject("limit", pageSize);
        result.addObject("total", total);
        result.addObject("current", current);
        if (total > 0) {
            int page = (int) Math.ceil(total / pageSize);
            double totald = total;
            if (totald / pageSize > total / pageSize) {
                page = page + 1;
            }
            result.addObject("page", page);
            int startPage = 0;
            int endPage = 0;
            if (page < 8) {
                startPage = 1;
                endPage = page;
            } else {
                if (current < 5) { //展示前6页
                    startPage = 1;
                    endPage = 6;//page>6?6:page; 
                } else if (page - current < 6) {//展示最后6页，例如：current=10 page=15   10-15
                    startPage = page - 5;
                    endPage = page;
                } else {//current=20    18——22
                    startPage = current - 2;
                    endPage = current + 2;
                }
            }
            result.addObject("startPage", startPage);
            result.addObject("endPage", endPage);
        } else {
            result.addObject("page", 0);
        }
        return result;
    }


    public String  getValidResult(BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            List<String> msgList = new ArrayList<String >();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for(ObjectError objectError :allErrors){
                String defaultMessage = objectError.getDefaultMessage();
                msgList.add(defaultMessage);
            }
            return msgList.toString();
        }
        return null;
    }
}  